package service.impl;

import dao.UserDao;
import dao.factory.UserDaoFactory;
import dao.impl.UserDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.User;
import service.UserService;

import javax.annotation.Resource;

/**
 * 用户模块业务层实现
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    //通过工厂获取所依赖的UserDao对象
    //使用此注解时，若找不到相匹配的类，Spring容器会抛出异常。为避免抛出异常，将required属性设置为false
    @Autowired(required = false)
//    @Resource(name = "userDao")
//    @Resource(type = UserDaoImpl.class)
    private UserDao dao;
//    //实例化所依赖的UserDao对象
//    private UserDao dao = new UserDaoImpl();

    /**
     * 保存用户信息
     * @param user
     */
    @Override
    public void save(User user) {
        //调用UserDao的方法保存用户信息
        dao.save(user);
    }
    /**
     * dao的属性setter访问器，会被Spring调用，实现依赖注入
     */
    public void setDao(UserDao dao) {
        this.dao = dao;
    }

    public UserServiceImpl() {
    }

    public UserServiceImpl(UserDao dao) {
        this.dao = dao;
    }
}
